package cn.nfwx.tv.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.nfwx.tv.R;

public class ItemSettingView extends LinearLayout {
    private String text;
    private int iconRid;
    private ImageView iv_setting_icon;
    private TextView tv_setting_text;

    public ItemSettingView(Context context) {
        this(context, null);
    }

    public ItemSettingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("Recycle")
    public ItemSettingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_item_setting, this, true);

        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.ItemSettingView);
        if (t.hasValue(R.styleable.ItemSettingView_settingicon)) {
            iconRid = t.getResourceId(R.styleable.ItemSettingView_settingicon, 0);
        }
        if (t.hasValue(R.styleable.ItemSettingView_settingname)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (t.getType(R.styleable.ItemSettingView_settingname) == TypedValue.TYPE_STRING) {
                    text = t.getString(R.styleable.ItemSettingView_settingname);
                } else if (t.getType(R.styleable.ItemSettingView_settingname) == TypedValue.TYPE_REFERENCE) {
                    int rid = t.getResourceId(R.styleable.ItemSettingView_settingname, -1);
                    if (rid != -1) {
                        text = context.getString(rid);
                    }
                }
            } else {
                text = t.getString(R.styleable.ItemSettingView_settingname);
                int rid = t.getResourceId(R.styleable.ItemSettingView_settingname, -1);
                if (rid != -1) {
                    text = context.getString(rid);
                }
            }
        }

        t.recycle();

        init();
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            tv_setting_text.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.sp18));
            tv_setting_text.getPaint().setFakeBoldText(true);
        } else {
            tv_setting_text.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.sp16));
            tv_setting_text.getPaint().setFakeBoldText(false);
        }
    }

    private void init() {
        iv_setting_icon = findViewById(R.id.iv_setting_icon);
        tv_setting_text = findViewById(R.id.tv_setting_text);

        iv_setting_icon.setImageResource(iconRid);
        tv_setting_text.setText(text);
    }
}
